package com.example.wxy.bbtapp.Service;


import android.content.Context;
import android.widget.Toast;

import com.example.wxy.bbtapp.Startup.DbService;
import com.example.wxy.bbtapp.Models.ClassTime;
import com.example.wxy.bbtapp.myApplication;

import org.xutils.ex.DbException;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by yons on 2017/9/9.
 */

public class ClassTimeService {

    private static int DownCount = 0;

    public static ClassTime GetCurrentClass(Context c)
    {
        ClassTime course = null;
        try
        {
            SimpleDateFormat sDateFormat= new SimpleDateFormat("HH:mm");
            String date= sDateFormat.format(new java.util.Date());
            course= DbService.getDB().selector(ClassTime.class).where("StartTime","<=",date).and("EndTime" ,">", date).findFirst();
            if(course == null && DownCount < 3 && myApplication.bindinofs.getSchoolID() != null && myApplication.bindinofs.getSchoolID().length() > 0)
            {
                LoadClassTime();
                DownCount ++;
            }
        }
        catch (DbException e)
        {
            Toast.makeText(c, "我发现好像学校作息时间表没有登记过哦，能帮我联系管理员吗？", Toast.LENGTH_LONG).show();
        }

        return course;
    }

    public static void LoadClassTime() {
        if (myApplication.Token != null)
            BindService.GetClassTime(myApplication.bindinofs.getSchoolID(), null);
    }

    public void Save(ClassTime data)throws DbException
    {
        DbService.getDB().saveBindingId(data);
    }

    public void Save(List<ClassTime> datas)throws DbException
    {
        DbService.getDB().delete(ClassTime.class);

        DbService.getDB().saveBindingId(datas);
    }
}
